package com.konsung.ksdc.common.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.Toast;

import com.konsung.ksdc.R;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.config.MyApplication;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 售后评价弹出框
 * Created by DJH on 2017/3/13 0013.
 */
public class AfterSaleEvaluate implements View.OnClickListener {
    @Bind(R.id.rb_evaluate)
    RatingBar rbEvaluate;
    @Bind(R.id.et_evaluate_content)
    EditText etEvaluateContent;
    @Bind(R.id.btn_close)
    Button btnClose;
    @Bind(R.id.btn_confirm)
    Button btnConfirm;

    private PopupWindow popupWindow;
    private View parent;
    private Context context;
    private String serviceId;
    private Handler handler;
    public static final int EVALUATED = 1; // 标示是否已评价
    private static final float BACKGROUND_ALPHA = 0.5f; // 背景透明度

    /**
     * 售后评价popupWindow构造方法
     * @param context 上下文
     * @param handler handler
     * @param parent 父控件
     * @param serviceId 售后服务id
     */
    public AfterSaleEvaluate(Context context, Handler handler, View parent, String serviceId) {
        this.context = context;
        this.handler = handler;
        this.parent = parent;
        this.serviceId = serviceId;
    }

    /**
     * 打开评价窗体
     */
    public void openWindow() {
        if (popupWindow == null) {
            View view = this.createPopupView();

            popupWindow = new PopupWindow(view,
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    setBackgroundAlpha(1f);
                }
            });
        }
        //设置软键盘弹出适应editText，如果不遮挡editText则不顶布局，如果遮挡则顶布局
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
        setBackgroundAlpha(BACKGROUND_ALPHA);
    }

    /**
     * 创建视图
     * @return view
     */
    private View createPopupView() {
        View view = LayoutInflater.from(context).inflate(R.layout
                .view_after_sale_evaluate, null);
        ButterKnife.bind(this, view);
        btnClose.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
        return view;
    }

    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha 背景透明度
     */
    public void setBackgroundAlpha(float bgAlpha) {
        Window window = MyApplication.mCurrentActivity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        window.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_close:
                popupWindow.dismiss();
                break;
            case R.id.btn_confirm:
                submitEvaluate();
                break;
            default:
                break;
        }
    }

    /**
     * 提交售后评价
     */
    private void submitEvaluate() {
        String evaluateLevel = String.valueOf((int) rbEvaluate.getRating());
        String content = etEvaluateContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            Toast.makeText(context, R.string.input_evaluate_content, Toast.LENGTH_SHORT).show();
            return;
        }
        RequestUtils.doSaleEvaluate(serviceId, evaluateLevel, content, new RequestCallback() {
            @Override
            public void onError(Call call, Exception e) {
            }

            @Override
            public void onResponse(ResponseBean responseBean) {
                if (Constants.REQUEST_OK.equals(responseBean.getResultCode())) {
                    Toast.makeText(context, R.string.submit_success, Toast.LENGTH_SHORT).show();
                    Message msg = new Message();
                    msg.what = EVALUATED;
                    handler.sendMessage(msg);
                }
            }
        });
        popupWindow.dismiss();
    }
}
